import { Component, OnInit, ViewChild } from '@angular/core';
import { environment } from '@env/environment';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { AuthDictService } from '../../../system/dict/service/dict.service';
import { TransportService } from '../../transport-service/transport.service';
import { BasicDataService } from '../../basic-data/basic-data.service';
import { SearchParamUtils } from '../../../../shared/utils/SearchParamUtils';
import { TallyCheckOstListComponent } from '../../tally-check/check/ost/list/list.component';
import { TallySearchExportOstListComponent } from '../ost-list/ost-list.component';
import { TallySearchExportTstListComponent } from '../tst-list/tst-list.component';
import { TallySearchExportBulkListComponent } from '../bulk-list/bulk-list.component';

@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.less']
})
export class TallySearchExportIndexComponent implements OnInit {

	company: string;

	@ViewChild('ostList')
	ostListComponent: TallySearchExportOstListComponent;

	@ViewChild('tstList')
	tstListComponent: TallySearchExportTstListComponent;

	@ViewChild('bulkList')
	bulkListComponent: TallySearchExportBulkListComponent;

	searchParams = {};
	dateRanges = environment.dateRanges;
	searchForm: FormGroup;
	shipLandList = [];
	timeSpaceList = [];
	tallyTypeList = [];
	shipList = [];
	billList = [];
	userList = [];
	locationList = [];
	positionList = [];
	corpTeamList = [];
	machineKindList = [];
	ynList = environment.dict.yesOrNoStr;
	workerTree: any;
	operationProcessList = [];
	holdList = [];

	constructor(
		private activatedRoute: ActivatedRoute,
		private fb: FormBuilder,
		private dictService: AuthDictService,
		private transportService: TransportService,
		private basicDataService: BasicDataService
	) {
		this.dictService.dictionary(environment.dict.SHIP_LAND, 1).subscribe(res => {
			this.shipLandList = res;
		});
		this.dictService.dictionary(environment.dict.TALLY_TYPE, 1).subscribe(res => {
			this.tallyTypeList = res;
		});
		this.dictService.dictionary(environment.dict.TIME_SPACE, 1).subscribe(res => {
			this.timeSpaceList = res;
		});
		this.dictService.dictionary(environment.dict.MACHINE_KIND, 1).subscribe(res => {
			this.machineKindList = res;
		});
		this.dictService.dictionary(environment.dict.HOLD, 1).subscribe(res => {
			this.holdList = res;
		});
		this.activatedRoute.params.subscribe(param => {
			this.company = param['company']?param['company']:null;
			this.basicDataService.teamTree().subscribe(res => {
				this.workerTree = res;
				if (this.company !== null) {
					this.workerTree = res.filter(i => i.transportCorpId === this.company);
				}
			});
		});

		this.basicDataService.operationProcessList({ isProcess: 'Y' }).subscribe(res => {
			this.operationProcessList = res;
		});
		this.transportService.transportShipList({}).subscribe(res => {
			this.shipList = res;
		});
		this.transportService.transportCorpTeamList({ hasMachine: 1 }).subscribe(res => {
			this.corpTeamList = res;
		});
		this.locationList = this.dictService.dictMainLocationInfoList;
		this.positionList = this.dictService.dictLocationInfoList;
		this.userList = this.dictService.tallyUserList;
		this.searchForm = this.fb.group({
			startAndEndTime: null,
			transportShipId: null,
			transportBillId: null,
			timeSpace: null,
			createUser: null,
			areaId: null,
			positionId: null,
			corpTeamId: null,
			machineKind: null,
			workerInfo: null,
			operationType: null,
			tallyType: null,
			shipLand: null,
			keeper: null,
			hold: null,
			isSafeProduction: null
		});
	}

	ngOnInit(): void {
		this.searchForm.valueChanges.subscribe(res => {
			const formatter = new SearchParamUtils();

			const startAndEndTimeFormatted = formatter.dateRangePickerFormat(res?.startAndEndTime);
			res.startTime = startAndEndTimeFormatted[0];
			res.endTime = startAndEndTimeFormatted[1];

			const workerInfoFormatted = formatter.workerInfoFormat(res?.workerInfo);

			this.searchParams = {
				...res,
				...workerInfoFormatted
			};
		});
	}

	loadData(): any {
		this.ostListComponent.loadData();
		this.tstListComponent.loadData();
		this.bulkListComponent.loadData();
	}

	search(): any {
		this.ostListComponent.pageInfo.current = 1;
		this.tstListComponent.pageInfo.current = 1;
		this.bulkListComponent.pageInfo.current = 1;
		this.loadData();
	}

	reset() {
		this.searchForm.reset();
		this.ostListComponent.resetParams();
		this.tstListComponent.resetParams();
		this.bulkListComponent.resetParams();
		this.loadData();
	}

	searchShipList(data: string) {
		this.transportService.transportShipList({ keyword: data }).subscribe(res => {
			this.shipList = res;
		});
	}

	searchBillList(data: any) {
		if (data) {
			this.transportService.transportBillList({ transportShipId: data }).subscribe(res => {
				this.billList = res;
			});
		}
	}

	setPositionList(data: any): void {
		this.searchForm.patchValue({ position: null });
		if (data) {
			this.positionList = this.dictService.dictLocationInfoList.filter((i) => i.areaId === data);
		} else {
			this.positionList = this.dictService.dictLocationInfoList;
		}
	}

	export() {
		const params = {
			preCompany: this.company,
			...this.searchParams
		}
		window.open(`${environment.FINE_REPORT_URL}handle-tally/gongbanchaxundaochu.cpt&__parameters__=${JSON.stringify(params)}`, '_blank');
	}
}
